<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<jsp:include page="/WEB-INF/jsp/inc/include-easyUI.jsp"></jsp:include>
	<!-- 弹出框 -->
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/content/sweetalert/dist/sweetalert.css">
	<script src="${pageContext.request.contextPath}/static/content/sweetalert/dist/sweetalert.min.js"></script>
	
	<link href="${pageContext.request.contextPath}/static/content/css/default.css" rel="stylesheet" type="text/css"/>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/content/js/outlook2.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/content/js/validDate.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/content/jquery-easyui-datagridview/datagrid-detailview.js"></script>
	
		<!-- 表单验证 -->
	<!--     <link rel="stylesheet" href="<%=request.getContextPath()%>/static/content/bootstrapValidator/vendor/bootstrap/css/bootstrap.css"/> -->
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/content/bootstrapValidator/dist/css/bootstrapValidator.css"/>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/content/bootstrapValidator/vendor/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/content/bootstrapValidator/dist/js/bootstrapValidator.js"></script>
	
	<script src="${pageContext.request.contextPath}/static/assets/js/bootstrap.min.js"></script>
<style type="text/css">
  .sweet-alert h2 {
    color: rgb(87, 87, 87);
    font-size: 30px;
    text-align: center;
    font-weight: 600;
    text-transform: none;
    position: relative;
    line-height: 40px;
    display: block;
    margin: 25px 0px;
    padding: 0px;
}
 .sweet-alert p {
    color: rgb(121, 121, 121);
    font-size: 16px;
    font-weight: 300;
    position: relative;
    text-align: inherit;
    float: none;
    line-height: normal;
    margin: 0px;
    padding: 0px;
}
.sweet-alert .sa-error-container {
    background-color: rgb(241, 241, 241);
    margin-left: -17px;
    margin-right: -17px;
    max-height: 0px;
    overflow: hidden;
    padding: 0px 10px;
    transition: padding 0.15s, max-height 0.15s;
}
.sweet-alert button.cancel {
    background-color: rgb(193, 193, 193);
}
.sweet-alert button {
    background-color: rgb(140, 212, 245);
    color: white;
    box-shadow: none;
    font-size: 17px;
    font-weight: 500;
    cursor: pointer;
    border: none;
    border-radius: 5px;
    padding: 10px 32px;
/*     margin: 26px 30px 0px; */
/*     width: 150px; */
}
.sweet-alert .sa-confirm-button-container {
    display: inline-block;
    position: relative;
/*     padding-left: 20px; */
}
.cancel{
	margin-top: 15px;
	margin-left: 50px;
}
.confirm{
	margin-top: 15px;
	margin-left: 150px;
}
.glyphicon {
	position: relative;
	top: -23px;
	padding-right: 10px;
	display: inline-block;
	font-family: 'Glyphicons Halflings';
	-webkit-font-smoothing: antialiased;
	font-style: normal;
	font-weight: normal;
	line-height: 1;
	float: right;
}

.help-block {
	display: block;
	margin-top: 10px;
	margin-bottom: 0px;
	color: #737373;
}
.widget-toolbar>a {
    font-size: 36px;
    margin: 0 1px;
    display: inline-block;
    padding: 0;
/*     line-height: 24px; */
}
.form-horizontal {
    margin-bottom: 0px;
}
</style>
</head>

<body>
	<div class="main-content">
<!-- 		<div class="breadcrumbs" id="breadcrumbs"> -->
<!-- 			<script type="text/javascript"> -->
<!-- 				try { -->
<!-- 					ace.settings.check('breadcrumbs', 'fixed') -->
<!-- 				} catch (e) { -->
<!-- 				} -->
<!-- 			</script> -->
<!-- 			<ul class="breadcrumb"> -->
<!-- 				<li><i class="icon-home home-icon"></i> <a -->
<!-- 					href="javascript:void(0);">首页</a></li> -->
<!-- 				<li class="active">欢迎页面</li> -->
<!-- 			</ul> -->
<!-- 			.breadcrumb -->
<!-- 			<div class="nav-search" id="nav-search"> -->
<!-- 				<form class="form-search"> -->
<!-- 					<span class="input-icon"> <input type="text" -->
<!-- 						placeholder="Search ..." class="nav-search-input" -->
<!-- 						id="nav-search-input" autocomplete="off" /> <i -->
<!-- 						class="icon-search nav-search-icon"></i> -->
<!-- 					</span> -->
<!-- 				</form> -->
<!-- 			</div> -->
<!-- 			#nav-search -->
<!-- 		</div> -->
		<div class="page-content">
			<div class="page-header">
				<h1>部门管理</h1>
			</div>
			<!-- /.page-header -->
			
			<!-- 创建部门 -->
			<div class="modal fade" id="addDepartmentModal" tabindex="-1" role="dialog" aria-labelledby="addDepartmentModalLabel"  >
			  <div class="modal-dialog" role="document" style="margin:130px 350px;">
			    <div class="modal-content">
					<form id="addDepartmentInfoForm" class="form-horizontal" role="form">
						<div class="modal-header">
<!-- 							<button type="button" class="close" data-dismiss="modal" aria-label="Close"> -->
<!-- 								<span aria-hidden="true">&times;</span> -->
<!-- 							</button> -->
							<h4 class="modal-title" id="addDepartmentModalLabel">部门信息</h4>
						</div>
						<div class="modal-body">
							<div class="space-4"></div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right" for="add-department-parentDept"> 上级部门：</label>
								<div class="col-sm-8">
									<input type="text" class="form-control" name="parentDept" id="add-department-parentDept" placeholder="上级部门"/>
								</div>
							</div>
							<div class="space-8"></div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right" for="add-department-name"> 部门名称：</label>
								<div class="col-sm-8">
									<input type="text" class="form-control" name="name" id="add-department-name" placeholder="部门名称"/>
								</div>
							</div>
							<div class="space-8"></div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right" for="add-department-description"> 部门描述： </label>
								<div class="col-sm-8">
									<textarea class="form-control" name="description" id="add-department-description" placeholder="部门描述"></textarea>
								</div>
							</div>
						</div>
						<div class="modal-footer">
							<div class="col-lg-6 col-lg-offset-5">
								<button type="button" class="btn btn-default" data-dismiss="modal" id="reset_addDepartmentInfo">关闭</button>
								<button type="submit" class="btn btn-primary" data-dismiss="modal" id="submit_addDepartmentInfo">确定</button>
							</div>
						</div>
					</form>
			    </div>
			  </div>
			</div>
			
			
			
			<div class="row">
				<div class="col-xs-12">
					<!-- PAGE CONTENT BEGINS -->
					
					<table id="department"></table>
					
					<!-- PAGE CONTENT ENDS -->
				</div>
				<!-- /.col -->
			</div>
			<!-- /.row -->
		</div>
		<!-- /.page-content -->
	</div>
	<!-- /.main-content -->

	<script type="text/javascript">
        $(function () {
            var departmentGrid=$('#department').treegrid({
                title: '部门',
                url: '${pageContext.request.contextPath}/sys/department/treeGrid',
                idField: 'id',
                treeField: 'name',
                fitColumns:true,
                rownumbers: true,
                toolbar: [
                    {
                        text: '创建',
                        iconCls: 'icon-add',
                        handler: function () {
                            createDepart();
                        }
                    }, '-', {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: function () {
                        	deleteDepart();
                        }
                    }, '-', {
                        text: '编辑',
                        iconCls: 'icon-edit',
                        handler: function () {
                        	editDepart();
                        }
                    }, '-', {
                        text: '取消选中',
                        iconCls: 'icon-undo',
                        handler: function () {
                        	departmentGrid.datagrid('unselectAll');
                        }
                    }],
                frozenColumns: [[
	                      { field: 'ck', checkbox: true }
                        , { field: 'name', title: '名称', width: 240 }
				    ]],
                columns: [[
					  { field: 'desc', title: '描述', width: 200 }, 
					  { field: 'createTime', title: '创建时间', width: 120 }, 
					  { field: 'updateTime', title: '更新时间', width: 120 }
				    ]]
            });

//             $("#deptParent").combotree({
//                 url: '/DepartmentTree/GetTree',
//                 onShowPanel: function () {
//                     $("#deptParent").combotree('reload');
//                 }
//             });
        });
        //提示选择信息
        function getSelectId() {
            var node = $('#department').treegrid('getSelected');
            if (!node) {
                $.messager.show({
                    title: '提示',
                    msg: '请选择数据！',
                    showType: 'show'
                });
                return null;
            } else {
                return node.Id;
            }
        }
        
        //创建部门
        function createDepart(){
        	var parentId=0;
        	var node=$('#department').treegrid('getSelected');
        	if(node!=null){
        		parentId=node.id;
        	}
        	var fromUrl = '${pageContext.request.contextPath}/department/toCreateDepartment?parentId='+parentId;
        	
        	$('#addDepartmentModal').modal('show');
        	$('#submit_addDepartmentInfo').click(function(){
        		var isValid = $('#addDepartmentInfoForm').data('bootstrapValidator').isValid();
				if(isValid){
					var toUrl='${pageContext.request.contextPath}/department/CreateDepartment';
					var f = p.find('form');
                    f.form('submit', {
                        url: toUrl,
                        onsubmit: function () {
                            var flag = $(this).form('validate');
                            if (flag) {
                           	 top.showProcess(true, '温馨提示', '正在提交数据...');
                            }
                            return flag;
                        },
                        success: function (data) {
                       	 top.showProcess(false);
                            var map = $.parseJSON(data);
                            if (map.success) {
                           	 top.showMsg('提示', map.msg);
                                reloadDataGrid();
                                p.dialog('close');
                            }
                            else {
                           	 top.alertMsg('错误', map.msg+"\n"+map.obj==null?"":map.obj);
                            }

                        },
                        onLoadError: function () {
                       	 top.showProcess(false);
                            $.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
                        }
                    });
				}
        	});
        	
             var p = top.sy.dialog({
                 title: '创建部门',
                 iconCls: 'icon-add',
                 href: fromUrl,
                 width: 500,
                 height: 400,
                 modal: true,
                 minimizable: false,
                 maximizable: false,
                 cache: false,
                 buttons: [{
                     text: '确定',
                     iconCls: 'icon-ok',
                     handler: function () {
                         var f = p.find('form');
                         f.form('submit', {
                             url: toUrl,
                             onsubmit: function () {
                                 var flag = $(this).form('validate');
                                 if (flag) {
                                	 top.showProcess(true, '温馨提示', '正在提交数据...');
                                 }
                                 return flag;
                             },
                             success: function (data) {
                            	 top.showProcess(false);
                                 var map = $.parseJSON(data);
                                 if (map.success) {
                                	 top.showMsg('提示', map.msg);
                                     reloadDataGrid();
                                     p.dialog('close');
                                 }
                                 else {
                                	 top.alertMsg('错误', map.msg+"\n"+map.obj==null?"":map.obj);
                                 }

                             },
                             onLoadError: function () {
                            	 top.showProcess(false);
                                 $.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
                             }
                         });

                     }
                 }, {
                     text: '取消',
                     iconCls: 'icon-cancel',
                     handler: function () {
                         p.dialog('close');
                     }
                 }]
             });
        }
        
        //编辑部门
        function editDepart() {
        	  var node=$('#department').treegrid('getSelected');
              if (node!=null) {
//                   if(node.parentId==null)
//                   {
//                      top.showMsg("提示", "该部门不能编辑！");
//                      return;
//                   }
                      //弹出编辑框
                 	 var url='${pageContext.request.contextPath}/department/toEditDepartment?deptId='+node.id;
                      var p = top.sy.dialog({
                          title: '编辑部门',
                          iconCls: 'icon-edit',
                          href: url,
                          width: 500,
                          height: 400,
                          modal: true,
                          minimizable: false,
                          maximizable: false,
                          cache: false,
                          buttons: [{
                              text: '确定',
                              iconCls: 'icon-ok',
                              handler: function () {
                                  var f = p.find('form');
                                  f.form('submit', {
                                      url: '${pageContext.request.contextPath}/department/EditDepartment',
                                      onsubmit: function () {
                                          var flag = $(this).form('validate');
                                          if (flag) {
                                        	  top.showProcess(true, '温馨提示', '正在提交数据...');
                                          }
                                          return flag;
                                      },
                                      success: function (data) {
                                    	  top.showProcess(false);
                                          var map = $.parseJSON(data);
                                          if (map.success) {
                                        	  top.showMsg('提示', map.msg);
                                              reloadDataGrid();
                                              p.dialog('close');
                                          }
                                          else {
                                        	  top.alertMsg('错误', map.msg+"\n"+map.obj==null?"":map.obj);
                                          }

                                      },
                                      onLoadError: function () {
                                    	  top.showProcess(false);
                                          $.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
                                      }
                                  });

                              }
                          }, {
                              text: '取消',
                              iconCls: 'icon-cancel',
                              handler: function () {
                                  p.dialog('close');
                              }
                          }]
                      });
                      }
                
              else {
                  showMsg("提示", "请选择要编辑的记录！");
              }  

        }
        
        //删除部门
        function deleteDepart(){
        	var node=$('#department').treegrid('getSelected');                
            if (node!=null) {
//              if(node.parentId==null)
//                   {
//                      top.showMsg("提示", "该部门不能删除！");
//                      return;
//                   }
            	top.$.messager.confirm('提示', '确定要删除吗？',
						function(r) {
							if (r) {
                        $.ajax({
								url : '${pageContext.request.contextPath}/department/deleteDepartment',
								data : {
									departmentId : node.id
								},
								cache : false,
								dataType : "json",
								success : function(data) {
									if (data.success) {
									$('#department').treegrid('unselectAll');
										reloadDataGrid();
										top.showMsg('提示', data.msg);
									} else {
										top.alertMsg('警告', data.msg+"\n"+data.obj==null?"":data.obj);
									}
								}
							});
							}
            	});
                    }
            else {
                showMsg("提示", "请选择要删除的记录！");
            }  
                
        }
        function getView() {
            var deptId = getSelectId();
            if (deptId) {
                var url = "${pageContext.request.contextPath}/Department/Details/"
						+ deptId;
				//添加新页面
				top.addTab("部门详细", url, "icon-search");
			}
		}

		function reload() {
			var node = $('#department').treegrid('getSelected');
			if (node) {
				$('#department').treegrid('reload', node.code);
			} else {
				$('#department').treegrid('reload');
			}
		}

		function remove() {
			var node = $('#department').treegrid('getSelected');
			if (node) {
				$('#department').treegrid('remove', node.Id);
			}
		}

		function reloadDataGrid() {
			$('#department').treegrid('clearChecked');
			$('#department').treegrid('reload');
		}
	</script>

</body>
</html>
